package cloudspace.vm.io.console;

import java.util.Iterator;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;

/**
 * The Hbox that represents all of the different visibility options for the
 * console vbox.
 */
public class VisToolbar extends Menubar
{
	public class AutoScrollHandler implements EventListener
	{
		ConsoleLines consoleBox;
		public AutoScrollHandler(ConsoleLines lines)
		{
			consoleBox = lines;
		}
		public void onEvent(Event event) throws Exception
		{
			Menuitem eventItem = (Menuitem) event.getTarget();
			eventItem.setChecked(!eventItem.isChecked());
			consoleBox.notifyAutoScrollChange();
		}
	}
	/**
	 * The Class TimeVisChangeHandler. This handles a change to time stamp
	 * visibility.
	 */
	public class TimeVisChangeHandler implements EventListener
	{

		/** The console box. */
		ConsoleLines consoleBox;

		/**
		 * Instantiates a new time vis change handler.
		 * 
		 * @param fellow
		 *            the master Console Vertical Box
		 */
		public TimeVisChangeHandler(ConsoleLines fellow)
		{
			consoleBox = fellow;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
		 * .Event)
		 */
		@Override
		public void onEvent(Event event) throws Exception
		{
			Menuitem eventItem = (Menuitem) event.getTarget();
			eventItem.setChecked(!eventItem.isChecked());
			consoleBox.notifyTimeStampChange();

		}

	}

	/**
	 * The Class VisChangeHandler. This handles a change in visibility of a
	 * stream.
	 */
	public class VisChangeHandler implements EventListener
	{

		/** The Console Vbox parent */
		ConsoleLines conComp;

		/**
		 * Instantiates a new vis change handler.
		 * 
		 * @param consoleComponent
		 *            the console component
		 */
		public VisChangeHandler(ConsoleLines consoleComponent)
		{
			conComp = consoleComponent;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
		 * .Event)
		 */
		@Override
		public void onEvent(Event event) throws Exception
		{
			Menuitem eventItem = (Menuitem) event.getTarget();
			eventItem.setChecked(!eventItem.isChecked());
			boolean isChecked = eventItem.isChecked();
			conComp.notifyVisChange(eventItem.getLabel(), isChecked);
		}

	}

	public class DistributedVisListener implements EventListener
	{

		/** The Console Vbox parent */
		ConsoleLines conComp;

		/**
		 * Instantiates a new vis change handler.
		 * 
		 * @param consoleComponent
		 *            the console component
		 */
		public DistributedVisListener(ConsoleLines consoleComponent)
		{
			conComp = consoleComponent;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
		 * .Event)
		 */
		@Override
		public void onEvent(Event event) throws Exception
		{
			Menuitem eventItem = (Menuitem) event.getTarget();
			eventItem.setChecked(!eventItem.isChecked());
			conComp.notifyDisVisChange(((Menuitem) event.getTarget())
					.isChecked());
		}

	}

	/** For Serialization. */
	private static final long serialVersionUID = -2117673001259154623L;

	/**
	 * This is notification from the console vbox that the stream provider has
	 * been updated or created. It is used when a new streamID is added.
	 * 
	 * @param evt
	 *            The event
	 */
	public void onUpdate(Event evt)
	{
		Menu vis = new Menu("Visibility");
		Menu options = new Menu("Options");
		this.appendChild(vis);
		this.appendChild(options);
		Menupopup visPop = new Menupopup();
		Menupopup optPop = new Menupopup();
		vis.appendChild(visPop);
		options.appendChild(optPop);
		Console sp = (Console) evt.getData();
		Iterator<String> idIter = sp.getStreamIDs();
		while (idIter.hasNext())
		{
			String streamID = idIter.next();
			createOption(visPop, streamID, new VisChangeHandler(
					(ConsoleLines) getFellow("consoleLines")), true);
			/*Menuitem tempBox = new Menuitem(streamID);
			tempBox.setId("_CSUI" + streamID);
			tempBox.setChecked(true);
			tempBox.addEventListener("onClick", new VisChangeHandler(
					(ConsoleLines) getFellow("consoleLines")));
			visPop.appendChild(tempBox);*/
		}
		createOption(optPop, "Hide Time stamp",  new TimeVisChangeHandler(
				(ConsoleLines) getFellow("consoleLines")),true);
		createOption(optPop, "Show All Prints", new DistributedVisListener(
				(ConsoleLines) getFellow("consoleLines")), false);
		//createOption(optPop, "Auto Scroll",new AutoScrollHandler((ConsoleLines) getFellow("consoleLines")), true );
		this.invalidate();
	}

	private void createOption(Menupopup optPop, String optionName, EventListener el, boolean isChecked)
	{
		Menuitem newItem = new Menuitem(optionName);
		newItem.setId("_CSUI"+optionName);
		newItem.addEventListener("onClick",el);
		newItem.setChecked(isChecked);
		optPop.appendChild(newItem);
	}
}
